package com.example.sellqq.controller;

import com.example.sellqq.domain.Record;
import com.example.sellqq.domain.User;
import com.example.sellqq.service.RecordService;
import com.example.sellqq.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author: thekernel
 * @Date: 2020/4/20 12:01
 * @Version: 1.0
 * @Description:
 */
@Api(value = "通话记录Controller")
@RestController
@RequestMapping("record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @ApiIgnore
    @GetMapping("hello")
    public @ResponseBody
    String index() {
        return "Hello Record !!!";
    }

    @ApiOperation(value = "添加多条通话记录信息", notes = "通话记录信息")
    @PostMapping("addAll")
    public @ResponseBody
    ResponseVO addMultiRecords(@Valid @RequestBody List<Record> records, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseVO.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        return this.recordService.saveRecords(records);
    }

    @ApiOperation(value = "添加单条通话记录信息", notes = "通话记录信息")
    @PostMapping("add")
    public @ResponseBody
    ResponseVO addRecord(@Valid @ModelAttribute Record record, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseVO.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        return this.recordService.saveRecord(record);
    }

    @ApiOperation(value = "读取所有通话记录", notes = "通话记录信息")
    @GetMapping("/all")
    public @ResponseBody
    ResponseVO readAllRecords() {
        return this.recordService.readAllRecords();
    }

}
